Use Span.Fill in ValueStringBuilder.Append for repeating chars - #131652
Open
prozolic wants to merge 1 commit into
Open
Use Span.Fill in ValueStringBuilder.Append for repeating chars#131652prozolic wants to merge 1 commit into
prozolic wants to merge 1 commit into
Conversation
This PR uses Span<char>.Fill in ValueStringBuilder.Append(char c, int count) and ValueStringBuilder<TChar>.Append(TChar c, int count) instead of a for loop to add each char. This change expected to improve performance for larger sizes.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR replaces manual per-element loops with Span<T>.Fill when appending a repeated character into ValueStringBuilder buffers, leveraging the framework’s optimized fill implementations to improve performance on larger repeat counts.
Changes:
- Update
ValueStringBuilder.Append(char c, int count)to use_chars.Slice(...).Fill(c)instead of aforloop. - Update
ValueStringBuilder<TChar>.Append(TChar c, int count)to use_chars.Slice(...).Fill(c)instead of aforloop.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/Common/src/System/Text/ValueStringBuilder.cs | Replaces the repeated-char append loop with Span<char>.Fill. |
| src/libraries/Common/src/System/Text/ValueStringBuilder_1.cs | Replaces the repeated-char append loop with Span<TChar>.Fill in the generic builder. |
prozolic
marked this pull request as ready for review
July 31, 2026 16:23
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
tannergooding
approved these changes
Jul 31, 2026
tannergooding
left a comment
Member
There was a problem hiding this comment.
LGTM. CC. @jeffhandley for whether the trivial 2 line perf improvement is ok to take into .NET 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR uses
Span<char>.FillinValueStringBuilder.Append(char c, int count)andValueStringBuilder<TChar>.Append(TChar c, int count)instead of a for loop to add each char.This change is expected to improve performance for larger sizes.
Benchmark
The benchmark uses
BigInteger.ToString/BigInteger.TryFormat, which internally useValueStringBuilder<TChar>.Append(TChar c, int count)Benchmark source